/*-------------------------------------------------------------------------
 * CLR版本:     4.0.30319.42000
 * 创建人:      song
 * 创建时间：   2024/4/9 17:56:20
 * 功 能：      KeyWord 绘制器
 *-------------------------------------------------------------------------*/

#if UNITY_EDITOR
namespace SCore.Editor.SShaderDraw
{
    using System.Collections.Generic;
    using UnityEditor;
    using UnityEngine;

    public class SKeyWordProp : SPropSuperBase<float>
    {
        private bool OpenKey;


        public SKeyWordProp(string name, GUIContent label, MaterialEditor materialEditor,
            Dictionary<string, MaterialProperty> props, bool OpenKey = true, GUIStyle gUIStyle = null) :
            base(name, label, materialEditor, props, 0f, gUIStyle)
        {
        }

        public SKeyWordProp(string name, GUIContent label, MaterialEditor materialEditor,
            Dictionary<string, MaterialProperty> props, float defaultValue, bool OpenKey = true,
            GUIStyle gUIStyle = null) :
            base(name, label, materialEditor, props, defaultValue, gUIStyle)
        {
            this.OpenKey = OpenKey;
        }

        protected override float getValue()
        {
            return base.props[name].floatValue;
        }

        protected override void ResetDefaultValue()
        {
            this.SetKeyWord(base.defaultValue > 0f);
        }

        protected override void setValue(float value)
        {
            base.props[name].floatValue = value;
        }

        public void SetKeyWord(bool state)
        {
            if (state)
            {
                this.setValue(1f);
                base.material.EnableKeyword(this.KeyWord);
            }
            else
            {
                this.setValue(0f);
                base.material.DisableKeyword(this.KeyWord);
            }
        }

        public override void Draw(float space, float BtnSize = 400)
        {
            if (base.Exist)
            {
                GUILayout.BeginHorizontal();
                GUILayout.Space(space);
                MaterialProperty prop = base.props[base.name];
                bool state = IsKeyWorldOn();
                state = EditorGUILayout.ToggleLeft(base.lable, state, guiStyle);
                GUILayout.EndHorizontal();
                this.SetKeyWord(state);
            }
        }

        protected bool IsKeyWorldOn()
        {
            return (base.Exist && base.material.IsKeywordEnabled(this.KeyWord));
        }

        public string KeyWord
        {
            get { return this.OpenKey ? (base.name.ToUpper() + "_ON") : base.name.ToUpper(); }
        }
    }
}

#endif